home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ASMCODE.ZIP / TYPES.PAS < prev   
Pascal/Delphi Source File  |  1994-11-02  |  11KB  |  426 lines

  1. { ────────────────────────────────────────────────────────────────────────
  2.  
  3.   This code is Copyright (c) 1994 by Jonathan E. Wright and AmoebaSoft.
  4.  
  5.   To communicate with the author, send internet mail to: NELNO@DELPHI.COM
  6.  
  7.   About this code:
  8.     This code was stripped from my normal global unit and error handler.
  9.     I hope I didn't screw anything up.
  10.  
  11.     If you use this code in any of your programs, or as a basis for anything
  12.     else you may write, please give credit to Nelno the Amoeba.  A postcard
  13.     from your country or town would also be nice.  Send it to:
  14.  
  15.     Nelno
  16.     58 1/2 Woodland Rd.
  17.     Asheville, NC 28804-3823
  18.     USA
  19.  
  20.   ────────────────────────────────────────────────────────────────────────
  21. }
  22.  
  23. UNIT Types;
  24.  
  25. Interface
  26.  
  27. USES
  28.   DOS;
  29.  
  30. CONST
  31.   DebugKeys : BOOLEAN = TRUE;
  32.  
  33.   hexChars: array [0..$F] of Char = '0123456789ABCDEF';
  34.  
  35.   DOSErrorMess : ARRAY [2..17] OF STRING [44] =
  36.     ('Could not locate the requested file.',
  37.      'Path not found.',
  38.      'Too many files open.',
  39.      'File access denied. ',
  40.      'Invalid file handle.', '', '', '', '', '',
  41.      'Invalid file access code.', '', '',
  42.      'Invalid drive number.',
  43.      'Cannot remove current directory.',
  44.      'Cannot rename accross drives.');
  45.  
  46.   CustErrorMess  : ARRAY [18..35] OF STRING [43] =
  47.     ('Could not perform memory request.',
  48.      'File has no palette.',
  49.      'File being saved contains color #255.',
  50.      'Entry not in library.',
  51.      'No EMM manager present.',
  52.      'Attempt to allocate EMMblock > 16384 bytes.',
  53.      'EMM free list is full in ',
  54.      'Too few pages to create requested EMM heap.',
  55.      'EMM manager version is below 4.0.',
  56.      'Attempt to read past end of file.',
  57.      'Sample larger than 65020 bytes.',
  58.      'No entries in library directory.',
  59.      'Unrecognizable MOD format.',
  60.      'Unknown format tag.',
  61.      '',
  62.      '',
  63.      '',
  64.      '');
  65.  
  66.   IOErrorMess  : ARRAY [100..106] OF STRING [24] =
  67.     ('Disk read error', 'Disk write error', 'File not assigned',
  68.      'File not open', 'File not open for input', 'File not open for output',
  69.      'Invalid numeric format');
  70.  
  71.   CriticalErrorMess : ARRAY [150..162] OF STRING [20] =
  72.     ('Disk is write-protected', 'Unknown unit',
  73.      'Drive not ready', 'Unknown command', 'CRC error in data',
  74.      'Disk seek error', 'Critical Error #155',
  75.      'Unknown media type', 'Sector Not Found', 'Printer out of paper',
  76.      'Device write fault', 'Device read fault', 'Hardware failure');
  77.  
  78.   FatalErrorMess : ARRAY [200..214] OF STRING [25] =
  79.     ('Division by zero', 'Range check error', 'Stack overflow error',
  80.      'Heap overflow error', 'Invalid pointer operation',
  81.      'Floating point overflow', 'Floating point underflow',
  82.      'Invalid F.L.O.P.', 'OVR manager not installed',
  83.      'Overlay file read error', 'Object not initialized',
  84.      'Call to abstract method', 'Fatal Error #212',
  85.      'Fatal Error #213', 'Fatal Error #214');
  86.  
  87. VAR
  88.   OldInt08        : POINTER;
  89.   OldInt1C        : POINTER;
  90.  
  91.   ErrorMessage    : STRING [80];
  92.   ErrorCode       : WORD;
  93.   ErrorAddress    : POINTER;
  94.  
  95. FUNCTION  ST (n : LONGINT): STRING;
  96. FUNCTION  Raise (n, x : INTEGER): LONGINT;
  97. FUNCTION  Exists (FileName : STRING) : BOOLEAN;
  98. PROCEDURE Print (S : STRING; Attribute : BYTE);
  99. FUNCTION  HexWord (w : WORD): STRING;
  100. FUNCTION  BinWord (n : WORD): STRING;
  101. FUNCTION  HexByte (b : BYTE): STRING;
  102. PROCEDURE ErrorHandler (UnitNum, n : WORD); FAR;
  103.  
  104. IMPLEMENTATION
  105.  
  106. VAR
  107.   SavedExit : POINTER;
  108.  
  109. { ╔═══════════════════════════════════════════════════════════════════════╗
  110.   ║                                                                       ║
  111.   ║                                                                       ║
  112.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  113.  
  114. PROCEDURE NewExit; FAR;
  115.  
  116. BEGIN
  117.   ExitProc := SavedExit;
  118. END;
  119.  
  120. { ╔═══════════════════════════════════════════════════════════════════════╗
  121.   ║                                                                       ║
  122.   ║                                                                       ║
  123.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  124.  
  125. FUNCTION ST (n : LONGINT): STRING;
  126.  
  127. VAR
  128.   S : STRING;
  129.  
  130. BEGIN
  131.   STR (n, S);
  132.   ST := S;
  133. END;
  134.  
  135. { ╔═══════════════════════════════════════════════════════════════════════╗
  136.   ║                                                                       ║
  137.   ║                                                                       ║
  138.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  139.  
  140. FUNCTION Raise (n, x : INTEGER): LONGINT;
  141.  
  142. VAR
  143.   Count : INTEGER;
  144.   n1    : INTEGER;
  145.  
  146. BEGIN
  147.   N1 := n;
  148.   IF x = 0 THEN
  149.     n := 0
  150.   ELSE
  151.     FOR Count := 1 to X - 1 DO
  152.       N := n * n1;
  153.  
  154.   Raise := n;
  155. END;
  156.  
  157. { ╔═══════════════════════════════════════════════════════════════════════╗
  158.   ║                                                                       ║
  159.   ║                                                                       ║
  160.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  161.  
  162. FUNCTION Exists (FileName : STRING) : BOOLEAN;
  163.  
  164. VAR
  165.   InFile : FILE OF BYTE;
  166.  
  167. BEGIN
  168.   ASSIGN (InFile, FileName);
  169.  
  170.   {$I-}
  171.   RESET (InFile);
  172.   {$I+}
  173.  
  174.   IF IOResult = 0 THEN
  175.     Exists := TRUE
  176.   ELSE
  177.     Exists := FALSE;
  178. END;
  179.  
  180. { ╔═══════════════════════════════════════════════════════════════════════╗
  181.   ║                                                                       ║
  182.   ║                                                                       ║
  183.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  184.  
  185. PROCEDURE Print (S : STRING; Attribute : BYTE);
  186.  
  187. VAR
  188.   R      : REGISTERS;
  189.   X, CY  : BYTE;
  190.   I      : INTEGER;
  191.   T      : CHAR;
  192.  
  193. BEGIN
  194.   R.AH := $03;   { get cursor position }
  195.   R.BH := 0;
  196.  
  197.   Intr ($10, R);
  198.  
  199.   X := R.DL;
  200.   CY := R.DH;
  201.  
  202.   FOR I := 1 to ORD (S [0]) DO
  203.   BEGIN
  204.     T := S [I];
  205.  
  206.     ASM
  207.       mov    ah,9
  208.       mov    al,T
  209.       mov    bl,Attribute
  210.       mov    bh,0
  211.       mov    cx,1
  212.       int    10h
  213.     END;
  214.  
  215.     INC (X);
  216.  
  217.     IF X > 80 THEN
  218.     BEGIN
  219.       X := 0;
  220.       INC (CY);
  221.       IF CY > 24 THEN
  222.       BEGIN
  223.         ASM
  224.           mov    ax,0601h
  225.           mov    cx,0101h
  226.           mov    dx,1950h
  227.           mov    bh,07h
  228.           int    10h
  229.  
  230.           mov    ah,2
  231.           mov    dl,0
  232.           mov    dh,24
  233.           mov    bh,0
  234.           int    10h
  235.  
  236.           mov    X,0
  237.           mov    CY,24
  238.         END;
  239.       END;
  240.     END;
  241.  
  242.     ASM
  243.       mov    ah,2
  244.       mov    dl,X
  245.       mov    dh,CY
  246.       mov    bh,0
  247.       int    10h
  248.     END;
  249.   END;
  250.  
  251.   INC (CY);
  252.   IF CY > 24 THEN
  253.   BEGIN
  254.     ASM
  255.       mov    ax,0601h
  256.       mov    cx,0101h
  257.       mov    dx,1950h
  258.       mov    bh,07h
  259.       int    10h
  260.  
  261.       mov    ah,02
  262.       mov    dl,0
  263.       mov    dh,24
  264.       mov    bh,0
  265.       int    10h
  266.  
  267.       mov    X,0
  268.       mov    CY,24
  269.     END;
  270.   END;
  271.   ASM
  272.     mov    ah,2
  273.     mov    bh,0
  274.     mov    dl,0
  275.     mov    dh,CY
  276.  
  277.     int    10h
  278.   END;
  279. END;
  280.  
  281. { ╔═══════════════════════════════════════════════════════════════════════╗
  282.   ║                                                                       ║
  283.   ║                                                                       ║
  284.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  285.  
  286. FUNCTION HexWord (w : WORD): STRING;
  287.  
  288. VAR
  289.   S : STRING;
  290.  
  291. BEGIN
  292.  
  293.  S := hexChars [Hi(w) shr 4] + hexChars [Hi(w) and $F] +
  294.       hexChars [Lo(w) shr 4] + hexChars [Lo(w) and $F];
  295.  
  296.  HexWord := S;
  297. END;
  298.  
  299. { ╔═══════════════════════════════════════════════════════════════════════╗
  300.   ║                                                                       ║
  301.   ║ FUNCTION BinWord (n : WORD): STRING;                                  ║
  302.   ║                                                                       ║
  303.   ╟───────────────────────────────────────────────────────────────────────╢
  304.   ║                                                                       ║
  305.   ║ returns a string containing the binary equivalent of the value of n   ║
  306.   ║                                                                       ║
  307.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  308.  
  309. FUNCTION BinWord (n : WORD): STRING;
  310.  
  311. VAR
  312.   I, Temp : WORD;
  313.   S       : STRING;
  314.  
  315. BEGIN
  316.   S := '                ';
  317.  
  318.   I := 16;
  319.  
  320.   WHILE (I > 0) DO
  321.   BEGIN
  322.     Temp := n MOD 2;
  323.     n := n DIV 2;
  324.     S [I] := CHR (Temp + 48);
  325.     DEC (I);
  326.   END;
  327.  
  328.   INSERT ('∙', S, 9);
  329.  
  330.   BinWord := S;
  331. END;
  332.  
  333. { ╔═══════════════════════════════════════════════════════════════════════╗
  334.   ║                                                                       ║
  335.   ║                                                                       ║
  336.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  337.  
  338. FUNCTION HexByte (b : BYTE): STRING;
  339.  
  340. VAR
  341.   S : STRING;
  342.  
  343. BEGIN
  344.  S := hexChars [b shr 4] + hexChars [b and $F];
  345.  
  346.  HexByte := S;
  347. END;
  348.  
  349. { ╔═══════════════════════════════════════════════════════════════════════╗
  350.   ║                                                                       ║
  351.   ║                                                                       ║
  352.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  353.  
  354. PROCEDURE ClrScr; ASSEMBLER;
  355.  
  356. ASM
  357.   mov     ah,02
  358.   xor     dx,dx
  359.   xor     bx,bx
  360.  
  361.   int     10h    { set cursor position }
  362.  
  363.   mov     ah,09
  364.   mov     al,20h
  365.   xor     bx,bx
  366.   mov     bl,07
  367.   mov     cx,2000
  368.  
  369.   int     10h
  370. END;
  371.  
  372. { ╔═══════════════════════════════════════════════════════════════════════╗
  373.   ║                                                                       ║
  374.   ║ Error handler for all units.                                          ║
  375.   ║                                                                       ║
  376.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  377.  
  378. PROCEDURE ErrorHandler (UnitNum, n : WORD);
  379.  
  380. BEGIN
  381.   ASM
  382.     mov     ax,[bp]                     { get return address from stack }
  383.     mov     dx,[bp+02]
  384.  
  385.     mov     word ptr ErrorAddress [0],ax
  386.     mov     word ptr ErrorAddress [2],dx
  387.   END;
  388.  
  389.   CASE n OF
  390.       2..17 : ErrorMessage := DOSErrorMess [n];
  391.       18..35: ErrorMessage := CustErrorMess [n];
  392.     100..106:
  393.           ErrorMessage := IOErrorMess [n];
  394.     150..162:
  395.           ErrorMessage := CriticalErrorMess [n];
  396.     200..214:
  397.           ErrorMessage := FatalErrorMess [n];
  398.     ELSE ErrorMessage := 'Unknown';
  399.   END;
  400.  
  401.   ErrorCode := n;
  402.  
  403.   Halt (UnitNum);
  404. END;
  405.  
  406. { ╔═══════════════════════════════════════════════════════════════════════╗
  407.   ║                                                                       ║
  408.   ║                                                                       ║
  409.   ╚═══════════════════════════════════════════════════════════════════════╝ }
  410.  
  411. VAR
  412.   I : INTEGER;
  413.  
  414. BEGIN
  415.   ErrorAddress := NIL;
  416.   ErrorCode := 0;
  417.   ErrorMessage := '';
  418.  
  419.   GetIntVec ($1C, OldInt1C);
  420.   GetIntVec ($08, OldInt08);
  421.  
  422.   SavedExit := ExitProc;
  423.   ExitProc := @NewExit;
  424.  
  425.   ClrScr;
  426. END.